/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package zhihuilan.controlle;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.alibaba.fastjson2.JSONObject;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import zhihuilan.common.WxMiniResponse;
import zhihuilan.common.enums.RedisKeyEnum;
import zhihuilan.common.utils.RedisCache;
import zhihuilan.common.utils.ToolsUtils;
import zhihuilan.entity.MyStaff;
import zhihuilan.entity.MyUser;
import zhihuilan.service.MyStaffService;

import javax.annotation.Resource;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

/**
 * @author chenk
 */
@Controller
public class BasicController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    protected MyStaffService myStaffService;

    @Resource
    protected RedisCache redisCache;

    protected MyStaff currentStaff = null;

    @Autowired
    protected WxMaService wxMaService;

    //    @ModelAttribute
    protected Boolean isCheckLogin(String guid) {
        currentStaff = null;
        if (ObjectUtils.isEmpty(guid)) {
            return false;
        }
        String key = RedisKeyEnum.REDIS_KEY_GUID + guid;
        currentStaff = redisCache.getCacheObject(key);
        if (ObjectUtils.isEmpty(currentStaff)) {
            this.currentStaff = myStaffService.selectMyStaffByGuid(guid);
            if (ObjectUtils.isEmpty(currentStaff)) {
                return false;
            }
            redisCache.setCacheObject(key, currentStaff, 1, TimeUnit.HOURS);
        }
        return true;
    }

    /**
     * 文本内容安全检测；
     *
     * */
    protected String isValidContent(String conetent) {
        if (!ObjectUtils.isEmpty(conetent)) {
            try {
                try {
                    String checkFt = ToolsUtils.verifymsg(conetent, currentStaff.getOpenid(), wxMaService);
                    JSONObject jsonObject = JSONObject.parseObject(checkFt);
                    String msg = "请文明言论";
                    if (ObjectUtils.isEmpty(jsonObject)) {
                        return null;
                    }
                    JSONObject resultObj = jsonObject.getJSONObject("result");
                    if (ObjectUtils.isEmpty(resultObj)) {
                        return null;
                    }
                    String suggestObj = resultObj.getString("suggest");
                    if (ObjectUtils.isEmpty(suggestObj)) {
                        return null;
                    }
                    if ("risky".equals(suggestObj)) {
                        String labelObj = resultObj.getString("label");
                        if (!ObjectUtils.isEmpty(labelObj)) {
                            if (!ObjectUtils.isEmpty(labelObj)) {
                                switch (labelObj) {
                                    case "10001":
                                        msg = "内容包含广告，" + msg;
                                        break;
                                    case "20001":
                                        msg = "内容包含时政，" + msg;
                                        break;
                                    case "20002":
                                        msg = "内容包含色情，" + msg;
                                        break;
                                    case "20003":
                                        msg = "内容包含辱骂，" + msg;
                                        break;
                                    case "20006":
                                        msg = "违法犯罪，" + msg;
                                        break;
                                    case "20008":
                                        msg = "内容包含欺诈，" + msg;
                                        break;
                                    case "20012":
                                        msg = "内容包含低俗，" + msg;
                                        break;
                                    case "20013":
                                        msg = "内容包含版权，" + msg;
                                        break;
                                    case "21000":
                                        msg = "其他违规，" + msg;
                                        break;
                                    default:
                                        return msg;
                                }
                            }
                        }
                        return msg;
                    }
                } catch (UnknownHostException e) {
                    return e.getMessage();
                }
            } catch (WxErrorException e) {
                return e.getMessage();
            }
        }
        return null;
    }
}
